import { ref, reactive, computed } from "vue";
import { useI18n } from "vue-i18n";
import { usePasteService } from "@/modules/paste";
import { copyToClipboard } from "@/utils/clipboard";
import { useAuthStore } from "@/stores/authStore.js";
import { useAdminBase } from "@/modules/admin";
import { generateQRCode as createQRCodeImage } from "@/utils/qrcodeUtils.js";

/** @typedef {import("@/types/paste").Paste} Paste */
/** @typedef {import("@/types/api").PaginationInfo} PaginationInfo */

/**
 * 文本管理 Admin composable
 * 基于 useAdminBase + usePasteService 统一管理 Admin 文本列表的加载、删除、搜索、预览、编辑等逻辑
 * @param {Object} options - 可选配置
 * @param {Function} options.confirmFn - 自定义确认函数，接收 {title, message, confirmType} 参数，返回 Promise<boolean>
 */
export function usePasteManagement(options = {}) {
  const { confirmFn } = options;

  // 国际化
  const { t } = useI18n();

  const base = useAdminBase('paste', {
    viewMode: {
      storageKey: 'paste-admin-view-mode',
      defaultMode: 'table',
    },
  });
  const pasteService = usePasteService();

  /** @type {import("vue").Ref<Paste[]>} */
  const pastes = ref([]);
  /** @type {import("vue").Ref<Paste | null>} */
  const previewPaste = ref(null);
  /** @type {import("vue").Ref<Paste | null>} */
  const editingPaste = ref(null);
  const showPreview = ref(false);
  const showEdit = ref(false);
  const showQRCodeModal = ref(false);
  const qrCodeDataURL = ref("");
  const qrCodeSlug = ref("");

  const copiedTexts = reactive(/** @type {Record<string, boolean>} */ ({}));
  const copiedRawTexts = reactive(/** @type {Record<string, boolean>} */ ({}));

  const authStore = useAuthStore();

  const isAdmin = computed(() => authStore.isAdmin);
  const isApiKeyUser = computed(() => authStore.authType === "apikey" && authStore.hasTextManagePermission);
  const isAuthorized = computed(() => isAdmin.value || isApiKeyUser.value);

  /**
   * 加载文本列表
   * @returns {Promise<void>}
   */
  const loadPastes = async () => {
    return base.withLoading(async () => {
      try {
        const { items, pagination } = await pasteService.getPastes({
          limit: base.pagination.limit,
          offset: base.pagination.offset,
        });

        pastes.value = items;
        base.updatePagination(pagination, "offset");
        base.updateLastRefreshTime();
        console.log(`文本列表加载完成，共 ${pastes.value.length} 条`);
      } catch (err) {
        pastes.value = [];
        throw err;
      }
    });
  };

  /**
   * offset 变更（分页）
   * @param {number} newOffset
   */
  const handleOffsetChange = (newOffset) => {
    base.handlePaginationChange(newOffset, "offset");
    loadPastes();
  };

  /**
   * 删除单条文本
   * @param {string|number} pasteId
   */
  const deletePaste = async (pasteId) => {
    if (!pasteId) {
      base.showError("删除失败：缺少文本标识信息");
      return;
    }

    // 使用传入的确认函数或默认的 window.confirm
    let confirmed;
    if (confirmFn) {
      confirmed = await confirmFn({
        title: t("common.dialogs.deleteTitle"),
        message: t("common.dialogs.deleteItem", { name: t("paste.item", "该文本") }),
        confirmType: "danger",
      });
    } else {
      confirmed = confirm(t("common.dialogs.deleteItem", { name: t("paste.item", "该文本") }));
    }

    if (!confirmed) {
      return;
    }

    return base.withLoading(async () => {
      await pasteService.deleteSinglePaste(pasteId);
      base.showSuccess("删除成功");
      await loadPastes();
    });
  };

  /**
   * 批量删除文本
   */
  const batchDeletePastes = async () => {
    const selectedCount = base.selectedItems.value.length;
    if (selectedCount === 0) {
      base.showError("请先选择要删除的文本");
      return;
    }

    // 使用传入的确认函数或默认的 window.confirm
    let confirmed;
    if (confirmFn) {
      confirmed = await confirmFn({
        title: t("common.dialogs.deleteTitle"),
        message: t("common.dialogs.deleteMultiple", { count: selectedCount }),
        confirmType: "danger",
      });
    } else {
      confirmed = confirm(t("common.dialogs.deleteMultiple", { count: selectedCount }));
    }

    if (!confirmed) {
      return;
    }

    return base.withLoading(async () => {
      await pasteService.deletePastes(base.selectedItems.value);
      base.showSuccess(`成功删除 ${selectedCount} 条文本`);
      base.clearSelection();
      await loadPastes();
    });
  };

  /**
   * 清理所有已过期文本（管理员）
   */
  const clearExpiredPastes = async () => {
    if (!isAdmin.value) {
      base.showError("只有管理员可以清理过期文本");
      return;
    }

    // 使用传入的确认函数或默认的 window.confirm
    let confirmed;
    if (confirmFn) {
      confirmed = await confirmFn({
        title: t("common.dialogs.cleanupTitle"),
        message: t("common.dialogs.cleanupExpired"),
        confirmType: "warning",
      });
    } else {
      confirmed = confirm(t("common.dialogs.cleanupExpired"));
    }

    if (!confirmed) {
      return;
    }

    return base.withLoading(async () => {
      const message = await pasteService.clearExpiredPastes();
      base.showSuccess(message || "已清理过期文本");
      await loadPastes();
    });
  };

  /**
   * 打开预览弹窗并加载完整内容
   * @param {Paste} paste
   */
  const openPreview = async (paste) => {
    try {
      // 加载完整的文本详情（包含 content 字段）
      const detail = await pasteService.getPasteById(paste.id);
      previewPaste.value = detail;
      showPreview.value = true;
    } catch (err) {
      console.error("获取文本详情失败:", err);
      base.showError("获取文本详情失败");
    }
  };

  const closePreview = () => {
    showPreview.value = false;
    previewPaste.value = null;
  };

  /**
   * 打开编辑弹窗并加载详情
   * @param {Paste} paste
   */
  const openEditModal = async (paste) => {
    try {
      const detail = await pasteService.getPasteById(paste.id);
      editingPaste.value = detail;
      showEdit.value = true;
    } catch (err) {
      console.error("获取文本详情失败:", err);
      base.showError("获取文本详情失败");
    }
  };

  const closeEditModal = () => {
    showEdit.value = false;
    editingPaste.value = null;
  };

  /**
   * 通用的文本更新方法
   * @param {string} pasteId - 文本ID
   * @param {Partial<Paste>} updates - 要更新的字段（支持部分更新）
   * @param {string} successMessage - 成功提示消息
   * @returns {Promise<void>}
   */
  const updatePasteFields = async (pasteId, updates, successMessage = "更新成功") => {
    return base.withLoading(async () => {
      try {
        // 获取完整数据（列表数据不包含content字段）
        const fullPaste = await pasteService.getPasteById(pasteId);

        // 合并更新数据（使用 ?? 空值合并，保持原值）
        const payload = {
          title: updates.title ?? fullPaste.title,
          content: updates.content ?? fullPaste.content,
          remark: updates.remark ?? fullPaste.remark,
          max_views: updates.max_views ?? fullPaste.max_views,
          expires_at: updates.expires_at ?? fullPaste.expires_at,
          is_public: updates.is_public ?? fullPaste.is_public,
        };

        // 处理特殊字段
        if (Object.prototype.hasOwnProperty.call(updates, "newSlug")) {
          payload.newSlug = updates.newSlug;
        }
        if (updates.password) {
          payload.password = updates.password;
        } else if (updates.clearPassword) {
          payload.clearPassword = true;
        }

        await pasteService.updatePaste(fullPaste.slug, payload);
        base.showSuccess(successMessage);
        await loadPastes();
      } catch (err) {
        console.error("更新文本失败:", err);
        base.showError(err.message || "更新文本失败");
        throw err;
      }
    });
  };

  /**
   * 提交编辑修改
   * @param {Partial<Paste>} updated
   */
  const submitEdit = async (updated) => {
    if (updated && updated.error) {
      base.showError(updated.error);
      return;
    }

    if (!editingPaste.value || !editingPaste.value.slug) {
      base.showError("提交失败：缺少文本标识");
      return;
    }

    try {
      await updatePasteFields(editingPaste.value.id, updated, "更新成功");
      closeEditModal();
    } catch (err) {
      // 错误已在 updatePasteFields 中处理
    }
  };

  /**
   * 复制访问链接
   * @param {string} slug - 文本标识
   */
  const copyLink = async (slug) => {
    if (!slug) {
      base.showError("复制失败：缺少文本标识");
      return;
    }

    const baseUrl = window.location.origin;
    const link = `${baseUrl}/paste/${slug}`;

    try {
      const ok = await copyToClipboard(link);
      if (ok) {
        copiedTexts[slug] = true;
        base.showSuccess("访问链接已复制");
        setTimeout(() => {
          copiedTexts[slug] = false;
        }, 2000);
      } else {
        base.showError("复制访问链接失败");
      }
    } catch (err) {
      console.error("复制访问链接失败:", err);
      base.showError("复制访问链接失败");
    }
  };

  /**
   * 复制 Raw 链接
   * @param {string} slug - 文本标识
   */
  const copyRawLink = async (slug) => {
    if (!slug) {
      base.showError("复制失败：缺少文本标识");
      return;
    }

    // 从列表中查找完整对象以获取 plain_password
    const pasteObj = pastes.value.find((item) => item.slug === slug);

    try {
      const rawLink = pasteService.getRawPasteUrl(slug, pasteObj?.plain_password || null);
      const ok = await copyToClipboard(rawLink);
      if (ok) {
        copiedRawTexts[slug] = true;
        base.showSuccess("Raw 链接已复制");
        setTimeout(() => {
          copiedRawTexts[slug] = false;
        }, 2000);
      } else {
        base.showError("复制 Raw 链接失败");
      }
    } catch (err) {
      console.error("复制 Raw 链接失败:", err);
      base.showError("复制 Raw 链接失败");
    }
  };

  /**
   * 快速编辑内容（双击编辑功能）
   * @param {Object} payload - { id, slug, content }
   */
  const quickEditContent = async (payload) => {
    if (!payload || !payload.id || !payload.slug || !payload.content) {
      base.showError("编辑失败：缺少必要参数");
      return;
    }

    try {
      // 获取完整数据（列表数据不包含content字段）
      const fullPaste = await pasteService.getPasteById(payload.id);

      // 合并更新数据（只更新 content 字段）
      const updatePayload = {
        title: fullPaste.title,
        content: payload.content,
        remark: fullPaste.remark,
        max_views: fullPaste.max_views,
        expires_at: fullPaste.expires_at,
        is_public: fullPaste.is_public,
      };

      // 调用 API 更新
      await pasteService.updatePaste(fullPaste.slug, updatePayload);

      // 局部更新：直接修改对象属性
      const index = pastes.value.findIndex((p) => p.id === payload.id);
      if (index !== -1) {
        // 直接修改属性而不是替换对象（避免触发整个列表重新渲染）
        pastes.value[index].content = payload.content;
      }

      base.showSuccess("内容已更新");
    } catch (err) {
      console.error("更新文本内容失败:", err);
      base.showError(err.message || "更新文本内容失败");
      throw err;
    }
  };

  /**
   * 打开公共访问页
   * @param {string} slug
   */
  const goToViewPage = (slug) => {
    const baseUrl = window.location.origin;
    const viewUrl = `${baseUrl}/paste/${slug}`;
    window.open(viewUrl, "_blank");
  };

  /**
   * 生成并显示二维码
   * @param {string} slug
   * @param {boolean} darkMode
   */
  const showQRCode = async (slug, darkMode = false) => {
    try {
      if (!slug) {
        base.showError("生成二维码失败：缺少文本标识信息");
        return;
      }

      const baseUrl = window.location.origin;
      const pasteUrl = `${baseUrl}/paste/${slug}`;

      qrCodeDataURL.value = await createQRCodeImage(pasteUrl, { darkMode });
      qrCodeSlug.value = slug;
      showQRCodeModal.value = true;
    } catch (err) {
      console.error("生成二维码失败:", err);
      base.showError("生成二维码失败");
    }
  };

  /**
   * 切换全选状态（按 id 字段）
   */
  const toggleSelectAll = () => {
    base.toggleSelectAll(pastes.value, "id");
  };

  /**
   * 搜索文本列表
   * @param {string} searchTerm
   * @param {number|null} [offset]
   * @returns {Promise<{results: Paste[], pagination: PaginationInfo}>}
   */
  const searchPastes = async (searchTerm, offset = null) => {
    if (!searchTerm || searchTerm.trim().length < 2) {
      return { results: [], pagination: /** @type {PaginationInfo} */ ({ total: 0, limit: base.pagination.limit, offset: 0 }) };
    }

    const searchOffset = offset !== null ? offset : base.pagination.offset;

    return base.withLoading(async () => {
      try {
        const { items, pagination } = await pasteService.getPastes({
          limit: base.pagination.limit,
          offset: searchOffset,
          search: searchTerm.trim(),
        });

        return {
          results: items,
          pagination: pagination || /** @type {PaginationInfo} */ ({
            total: items.length,
            limit: base.pagination.limit,
            offset: searchOffset,
          }),
        };
      } catch (err) {
        base.showError(err.message || "搜索失败");
        return {
          results: [],
          pagination: /** @type {PaginationInfo} */ ({
            total: 0,
            limit: base.pagination.limit,
            offset: searchOffset,
          }),
        };
      }
    });
  };

  /**
   * 切换文本可见性
   * @param {Paste} paste
   */
  const toggleVisibility = async (paste) => {
    if (!paste || !paste.id) {
      base.showError("操作失败：缺少文本标识");
      return;
    }

    const newVisibility = !paste.is_public;
    const visibilityText = newVisibility ? "公开" : "私密";

    try {
      // 复用通用更新方法，只更新 is_public 字段
      await updatePasteFields(paste.id, { is_public: newVisibility }, `已切换为${visibilityText}`);
    } catch (err) {
      // 错误已在 updatePasteFields 中处理
    }
  };

  /**
   * 关闭所有弹窗
   */
  const closeAllModals = () => {
    showPreview.value = false;
    showEdit.value = false;
    showQRCodeModal.value = false;
    previewPaste.value = null;
    editingPaste.value = null;
    qrCodeDataURL.value = "";
    qrCodeSlug.value = "";
  };

  return {
    // 继承 admin base
    ...base,

    // 文本管理状态
    pastes,
    previewPaste,
    editingPaste,
    showPreview,
    showEdit,
    showQRCodeModal,
    qrCodeDataURL,
    qrCodeSlug,
    copiedTexts,
    copiedRawTexts,

    // 权限状态
    isAdmin,
    isApiKeyUser,
    isAuthorized,

    // 操作
    loadPastes,
    searchPastes,
    handleOffsetChange,
    deletePaste,
    batchDeletePastes,
    clearExpiredPastes,
    openPreview,
    closePreview,
    openEditModal,
    closeEditModal,
    submitEdit,
    copyLink,
    copyRawLink,
    quickEditContent,
    goToViewPage,
    showQRCode,
    toggleSelectAll,
    toggleVisibility,
    closeAllModals,
  };
}
